package mina.forwhat.service;

import org.apache.hadoop.hdfs.web.JsonUtil;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.core.util.JsonUtils;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;

import antlr.StringUtils;
import mina.forwhat.bo.GameBO;

public class Demo1ServerHandler extends IoHandlerAdapter {

	private static Logger logger = Logger.getLogger(Demo1ServerHandler.class);
	 
	@Autowired
	GameBO gamebo;
	
	    @Override
	    public void sessionCreated(IoSession session) throws Exception {
	        logger.info("服务端与客户端创建连接...");
	    }
	 
	    @Override
	    public void sessionOpened(IoSession session) throws Exception {
	        logger.info("服务端与客户端连接打开...");
	    }
	 
	    @Override
	    public void messageReceived(IoSession session, Object message)
	            throws Exception {
	        String msg = message.toString();
	        logger.info("服务端接收到的数据为：" + msg);
	        //Date date = new Date();
	        logger.info("进入后台业务层处理。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。查找数据库数据");
	        String data =  gamebo.getAllDate().get(0).toString();
	        session.write(data);
	        session.close(true);
	    }
	    @Override
	    public void messageSent(IoSession session, Object message) throws Exception {
	        logger.info("服务端发送信息成功...");
	    }
	    @Override
	    public void sessionClosed(IoSession session) throws Exception {
	 
	    }
	    @Override
	    public void sessionIdle(IoSession session, IdleStatus status)
	            throws Exception {
	        logger.info("服务端进入空闲状态...");
	    }
	    @Override
	    public void exceptionCaught(IoSession session, Throwable cause)
	            throws Exception {
	        logger.error("服务端发送异常...", cause);
	    }

}
